package com.example.nio.utils;

import com.example.nio.commons.StatisReport;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class StatisReportUtil {
    //记录集合  String  用户id，StatisReport 记录对象
    private static Map<String, StatisReport> statisReportMap= new HashMap<>();

    /**
     * 添加记录
     * @param userName
     * @param department
     * @param agent
     * @param contect
     */
    public static void addStatisReport(String userName,String department,String agent,Integer contect,Integer messageSource){
        StatisReport statisReport = new StatisReport();
        statisReport.setStatisticsId(UUID.randomUUID().toString());
        if (messageSource == 1){
            statisReport.setChannel("web");
        }else{
            statisReport.setChannel("wechat");
        }
        statisReport.setDirection(1);
        statisReport.setCallReason("new");
        if(contect == 1){
            statisReport.setContect(contect);
            statisReport.setCallStart(new Date());
        }else if(contect == 3){
            statisReport.setContect(contect);
            statisReport.setListstart(new Date());
        }
        statisReport.setOpenId(userName);
        statisReport.setAgentId(agent);
        statisReport.setDepartment(department);
        System.out.println("添加接待记录，来访用户："+userName + ",记录：" + statisReport.toString());
        statisReportMap.put(userName,statisReport);
    }

    /**
     * 队列变更
     * @param userName
     * @param agentId
     */
    public static void changeList(String userName, String agentId){
        StatisReport statisReport=StatisReportUtil.getStatisReportByUserId(userName);
        if(statisReport != null){
            statisReport.setListend(new Date());
            Integer listalltime= statisReport.getMillisecBetweenDate(statisReport.getListstart(),statisReport.getListend());
            statisReport.setListalltime(listalltime);
            statisReport.setCallStart(new Date());
            statisReport.setAgentId(agentId);
            statisReportMap.put(userName,statisReport);
        }
    }

    /**
     * 挂断用户
     * @param userName
     * @param nickname
     */
    public static void cut(String userName,String nickname){
        statisReportMap.forEach((k,v) ->{
            if(k.equals(userName)){
                StatisReport statisReport = v;
                statisReport.setCallEnd(new Date());
                Integer callAllTime = statisReport.getMillisecBetweenDate(statisReport.getCallStart(),
                        statisReport.getCallEnd());
                statisReport.setCallAllTime(callAllTime).setNickName(nickname).setCloseReason(1);
            }
        });
    }

    public static StatisReport cut(String username){
        StatisReport statisReport = statisReportMap.get(username);
        statisReport.setCallEnd(new Date());
        Integer callAllTime = statisReport.getMillisecBetweenDate(statisReport.getCallStart(),
                statisReport.getCallEnd());
        statisReportMap.remove(username);
        return statisReport.setCallEnd(new Date()).setCallAllTime(callAllTime).setCloseReason(1);
    }
    /**
     * 根据用户id检索对象
     * @param userName
     * @return
     */
    public static StatisReport getStatisReportByUserId(String userName){
        return statisReportMap.get(userName);
    }

    /**
     * 根据用户id删除记录
     * @param userName
     */
    public static void closeStatisReport(String userName){
        statisReportMap.remove(userName);
    }

    /**
     * 根据用户id添加通话次数
     * @param userName
     */
    public static void addCallCount(String userName){
        statisReportMap.get(userName).setCallCount(statisReportMap.get(userName).getCallCount()+1);
    }
}
